using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MoneyManagerNET.Presentation.ViewModel
{
    using Model;
    using Infrastructure;
    using System.Collections.ObjectModel;
    using MoneyManagerNET.Presentation.ViewModel.Base;

    
    public class CategoryListViewModel:ListViewModel<ICategory>//ViewModelBase
    {

        public CategoryListViewModel():base( App.Storage.Select<ICategory>())
        {
            _NewCommand = new DelegateCommand(o => CreateNew(),o=>SubCategory==null);
            _EditCommand = new DelegateCommand(o=>EditCurrent(),o=>!(SubCategory==null && Category==null));
            _DeleteCommand = new DelegateCommand(o => DeleteCurrent(), o=>!(SubCategory == null && Category == null));

            this.PropertyChanged += (a, b) => { 
                if (b.PropertyName == string.Empty)
                { this.OnChange(() => Items); } 
            };


        }

        

        public ICategory Category { get; set; }
        public ISubCategory SubCategory { get; set; }

        object _selectedValue;
        public object SelectedValue {
            get {
                return _selectedValue;
            }
            set {
                _selectedValue = value;
                if (SelectedValue is ICategory) {
                    var selected_category=SelectedValue as ICategory;
                    this.Category = selected_category;
                    this.SubCategory = null;

                    this.CategoryName = selected_category.Name;
                }
                else if (SelectedValue is ISubCategory)
                {
                    var selected_subcategory=SelectedValue as ISubCategory;
                    this.SubCategory = selected_subcategory;
                    this.Category = this.SubCategory.Category as ICategory;

                    this.CategoryName = selected_subcategory.Name;
                }
                else {
                    this.Category = null;
                    this.SubCategory = null;
                }
                
                _EditCommand.RaiseCanExecuteChanged();
                _NewCommand.RaiseCanExecuteChanged();
                _DeleteCommand.RaiseCanExecuteChanged();
            }

        }

        #region Commands

        #region new
        DelegateCommand _NewCommand = null;
        public DelegateCommand NewCommand { get { return _NewCommand; } }

        void CreateNew()
        {
            if (SubCategory == null)
            {
                if (Category == null)
                {
                    var newCategory = App.Storage.New<ICategory>();
                    newCategory.Name = this.CategoryName;
                    Items.Add(newCategory);
                }
                else
                {
                    var newSubCategory = App.Storage.New<ISubCategory>();
                    newSubCategory.Name = this.CategoryName;
                    newSubCategory.Category = Category;
                    
                    RaiseCollectionChanged(Category.SubCategoryList);
                }
            }
            this.OnChange(() => ItemsView);
        }
        #endregion

        #region edit
        DelegateCommand _EditCommand = null;
        public DelegateCommand EditCommand { get { return _EditCommand; } }

        void EditCurrent()
        {
            if (Category != null)
            {
                if (SubCategory != null)
                {                    
                    SubCategory.Name = this.CategoryName;
                    RaisePropertyChanged(SubCategory, "Name");
                }
                else
                {
                    this.Category.Name=this.CategoryName;
                    RaisePropertyChanged(Category, "Name");
                }
            }
        }
        #endregion


        #region delete
        DelegateCommand _DeleteCommand = null;
        public DelegateCommand DeleteCommand { get { return _DeleteCommand; } }

        void DeleteCurrent()
        {
            if (Category != null)
            {
                if (SubCategory != null)
                {
                    this.Category.SubCategoryList.Remove(SubCategory);
                    base.RaiseCollectionChanged(Category.SubCategoryList);
                    App.Storage.Delete(SubCategory);
                }
                else
                {
                    App.Storage.Delete(Category);
                    base.Items.Remove(Category);
                    this.OnChange(() => ItemsView);
                }
            }
        }
        #endregion

        #endregion
        
        string _CategoryName;
        public string CategoryName { get { return _CategoryName; } set { _CategoryName = value; this.OnChange(() => CategoryName); } }

    }
}
